package com.oracle.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.mapper.MajorMapper;
import com.oracle.pojo.Major;
import com.oracle.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public IPage<Major> findMajor(Major major, Integer pageNum, Integer pageSize) {
        IPage<Major> page = new Page<>(pageNum, pageSize, true);
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(major)){
            IPage<Major> majorIPage = majorMapper.selectPage(page, null);
            return majorIPage;
        }else {
            String majorName = major.getMajorName();
            if (StringUtils.isEmpty(majorName)){
                IPage<Major> majorIPage = majorMapper.selectPage(page, null);
                return majorIPage;
            }else {
                queryWrapper.like("major_Name",majorName);
                IPage<Major> majorIPage = majorMapper.selectPage(page, queryWrapper);
                return majorIPage;
            }
        }
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public Major findOneMajor(Integer id) {
        Major major = majorMapper.selectById(id);
        return major;
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public List<Major> findAllMajor() {
        List<Major> majors = majorMapper.selectList(null);
        return majors;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int insertMajor(Major major) {
        int insert = majorMapper.insert(major);
        return insert;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updateMajor(Major major) {
        int i = majorMapper.updateById(major);
        return i;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int deleteMajor(Integer id) {
        int i = majorMapper.deleteById(id);
        return i;
    }
}
